package com.itheima.task;

import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class FrameworkTask {

    @Autowired
    private SetmealService setmealService;
    @Autowired
    private Configuration configuration;
    private static Integer i = 1;

    //@Scheduled(cron = "0/5 * * * * ?")
    @Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点触发一次
    public void frameworkInItTask() throws Exception{
        if(i == 1){
            System.out.println(LocalDate.now());
            generateSetmeal();
            generateSetmealDetail();
            i++;
        }
    }

    /**
     * 生成套餐列表模板
     * @throws Exception
     */
    public void generateSetmeal() throws Exception{
        // 创建FreeMarker配置
        configuration = new Configuration(Configuration.VERSION_2_3_28);
        configuration.setClassForTemplateLoading(this.getClass(), "/templates");

        //freemarker的模板对象，获取模板
        Template template = configuration.getTemplate("setmeal.ftl");
        //设置模型数据
        Map<String, Object> model = new HashMap<>();
        model.put("setmealList", setmealService.getSetmeal());
        //设置模板名称

        String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);

        File file = new File("bxg-health-mobile\\src\\main\\resources\\mobile\\pages\\setmeal.html");
        FileWriter writer = new FileWriter(file);
        writer.write(html);
        writer.close();
        System.out.println("加载framework-setmealList一次");
    }

    /**
     * 生成套餐列表详情模板
     * @throws Exception
     */
    public void generateSetmealDetail() throws Exception{
        //freemarker的模板对象，获取模板
        Template template = configuration.getTemplate("setmeal_detail.ftl");
        List<Setmeal> setmealList = setmealService.getSetmeal();
        for (Setmeal setmeal : setmealList) {
            //设置模型数据
            Map<String, Object> model = new HashMap<>();
            model.put("setmeal", setmealService.findById(setmeal.getId()));
            //设置模板名称

            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);

            File file = new File("bxg-health-mobile\\src\\main\\resources\\mobile\\pages\\setmeal_detail" + setmeal.getId() + ".html");
            FileWriter writer = new FileWriter(file);
            writer.write(html);
            writer.close();
            System.out.println("加载framework-setmeal_detail" + setmeal.getId() + "一次");
        }
    }
}
